﻿namespace VectorPicsEditor.Controls
{
    using System;
    using System.ComponentModel;
    using System.Windows.Controls;
    using System.Windows.Forms;
    using System.Windows.Media;
    using System.Windows.Shapes;
    using VectorPicsEditor.Helpers;

    public partial class ImageProperties : CommonProperties
    {
        public ImageProperties()
        {
            this.InitializeComponent();

            base.topTextBox.TextChanged += this.TopTextBoxTextChanged;
            base.leftTextBox.TextChanged += this.LeftTextBoxTextChanged;
            this.widthTextBox.TextChanged += this.WidthTextBoxTextChanged;
            this.heightTextBox.TextChanged += this.HeightTextBoxTextChanged;
            this.nameTextBox.LostFocus += this.ShapeNameChanged;
        }

        public override void Initialize(ShapeWrapper shape, System.Windows.Forms.ComboBox comboBox)
        {
            base.Initialize(shape, comboBox);
            this.InitFields();
            this.Shape.Shape.SizeChanged += this.RectangleSizeChanged;
            var topDescriptor = DependencyPropertyDescriptor.FromProperty(Canvas.TopProperty, typeof(Image));
            var leftDescriptor = DependencyPropertyDescriptor.FromProperty(Canvas.LeftProperty, typeof(Image));
            topDescriptor.AddValueChanged(this.Shape.Shape, this.RectanglePositionChanged);
            leftDescriptor.AddValueChanged(this.Shape.Shape, this.RectanglePositionChanged);
        }

        private void InitFields()
        {
            base.topTextBox.Text = Canvas.GetTop(this.Shape.Shape).ToString();
            base.leftTextBox.Text = Canvas.GetLeft(this.Shape.Shape).ToString();
            base.nameTextBox.Text = base.Shape.Name;
            this.widthTextBox.Text = base.Shape.Shape.Width.ToString();
            this.heightTextBox.Text = base.Shape.Shape.Height.ToString();

            this.opacityTrackBar.Value = (int)(this.Shape.Shape.Opacity * 100);
            this.zindexTextBox.Text = Canvas.GetZIndex(this.Shape.Shape).ToString();
        }

        private void RectanglePositionChanged(object sender, EventArgs e)
        {
            base.topTextBox.Text = Canvas.GetTop(this.Shape.Shape).ToString();
            base.leftTextBox.Text = Canvas.GetLeft(this.Shape.Shape).ToString();
            this.Shape.WasChanged = true;
        }

        private void RectangleSizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
        {
            this.widthTextBox.Text = base.Shape.Shape.Width.ToString();
            this.heightTextBox.Text = base.Shape.Shape.Height.ToString();
            this.Shape.WasChanged = true;
        }


        void ShapeNameChanged(object sender, EventArgs e)
        {
            if (this.Shape.Name != nameTextBox.Text)
            {
                this.Shape.Name = nameTextBox.Text;
                this.Shape.WasChanged = true;
                int index = this.ShapesComboBox.SelectedIndex;
                this.ShapesComboBox.Items[index] = this.Shape.Name;
            }
        }

        void LeftTextBoxTextChanged(object sender, EventArgs e)
        {
            try
            {
                double x = double.Parse(base.leftTextBox.Text);
                if (x != Canvas.GetLeft(this.Shape.Shape))
                {
                    this.Shape.Shape.SetValue(Canvas.LeftProperty, x);
                }
            }
            catch (FormatException)
            {
                this.RectanglePositionChanged(null, null);
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

        void TopTextBoxTextChanged(object sender, EventArgs e)
        {
            try
            {
                double y = double.Parse(base.topTextBox.Text);
                if (y != Canvas.GetLeft(this.Shape.Shape))
                {
                    this.Shape.Shape.SetValue(Canvas.TopProperty, y);
                }
            }
            catch (FormatException)
            {
                this.RectanglePositionChanged(null, null);
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

        void HeightTextBoxTextChanged(object sender, EventArgs e)
        {
            try
            {
                double x = double.Parse(this.heightTextBox.Text);
                if (x != base.Shape.Shape.Height)
                {
                    base.Shape.Shape.Height = x;
                }
            }
            catch (FormatException)
            {
                this.RectangleSizeChanged(null, null);
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

        void WidthTextBoxTextChanged(object sender, EventArgs e)
        {
            try
            {
                double x = double.Parse(this.widthTextBox.Text);
                if (x != base.Shape.Shape.Width)
                {
                    this.Shape.Shape.Width = x;
                }
            }
            catch (FormatException)
            {
                this.RectangleSizeChanged(null, null);
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }


        private void ChangeOpacity(object sender, EventArgs e)
        {
            this.Shape.Shape.Opacity = this.opacityTrackBar.Value / 100f;
            this.Shape.WasChanged = true;
        }

        private void ChangeZindex(object sender, EventArgs e)
        {
            try
            {
                int c = int.Parse(this.zindexTextBox.Text);
                Canvas.SetZIndex(this.Shape.Shape, c);
            }
            catch (FormatException)
            {
                this.zindexTextBox.Text = Canvas.GetZIndex(this.Shape.Shape).ToString();
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            //TODO: remove all events related to this shape
            this.Shape.Remove(this.Shape.Shape);
            int index = this.ShapesComboBox.SelectedIndex;
            if (index != 0)
                this.ShapesComboBox.SelectedIndex = index - 1;
            else
                this.Dispose();
            this.ShapesComboBox.Items.RemoveAt(index);
        }
    }
}
